<?php
/**
 * 后台登录页面
 * @author caoge
 * @version 2012-7-22
 */
class LoginAction extends Action{
	/**
	 * 登录页面显示
	 * 
	 * @author caoge
	 * @version 2012-7-22
	 */
	public function index(){
		if ($_SESSION['uid']){
			redirect(__APP__.'/Article/edit/');
		}
		$this->display();
	}
	
	/**
	 * 登录方法
	 * 
	 * @author caoge
	 * @version 2012-7-22
	 */
	public function login(){
		$userName = $this->_POST('username','','');
		$password = $this->_POST('password','','');
		$password = md5($password);
		$data['user_name'] = $userName;
		$data['password'] = $password;
		$data['del_flag'] = 0;
		$User = D(User);
		$ret = $User->where($data)->find();
		if($ret){
			$_SESSION['uid'] = $userName;
			$this->success('登录成功',__APP__.'/Article/index/');
		}else {
			$this->error('登录失败',__APP__.'/Login/index/');
		}
	}
	
	/**
	 * 退出登录状态
	 * 
	 * @author caoge
	 * @version 2012-7-22
	 */
	public function logout(){
		unset($_SESSION['uid']);
		$this->success('退出成功',__APP__.'/Login/index/');
	}
	
	/**
	 * 异步登录验证
	 * 
	 * @author caoge
	 * @version 2012-7-22
	 */
	public function ajaxLoginCheck(){
		$userName = $this->_POST('username','','');
		$password = $this->_POST('password','','');
		$password = md5($password);
		$data['user_name'] = $userName;
		$data['password'] = $password;
		$data['del_flag'] = 0;
		$User = D(User);
		$ret = $User->where($data)->find();
		$arrayToJs['status'] = 1;
		if (!$ret){
			$arrayToJs['status'] = 0;
			$arrayToJs['err_msg'] = '账号或者密码错误';
		}
		echo json_encode($arrayToJs);
	}
}